package com.zzh.myapplication.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.icarplus.car.R;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.tools.JsonUtil;
import net.icarplus.car.tools.LoadingUtil;
import net.icarplus.car.tools.S;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.T;
import net.icarplus.car.tools.ToolBarHelper;
import net.icarplus.car.tools.UserUtils;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * 项目名称：icarplus-car-android
 * 类描述：
 * 创建人：Gophy
 * 创建时间：16/3/7 18:59
 * 修改人：Gophy
 * 修改时间：16/3/7 18:59
 * 修改备注：
 */
public class VehicleControlActivity extends BaseActivity {

    private Toolbar toolbar;
    private TextView tv_return_car;
    private LinearLayout ivUnLock;
    private LinearLayout ivLock;
    private String appno;
    private String licenseNo;
    private String pay_status;
    private RelativeLayout rlBackground;

    private LoadingUtil loadingUtil;
    private boolean isClose = false;
    private boolean isOpen = false;


    @Override
    public void setContent() {
        GetObtainCar();
    }

    @Override
    public void initComponent() {
    }

    @Override
    public void setListener() {
    }

    @Override
    public void onAfterInit() {
    }

    public void Obtain_car(String date) {


        try {
            JSONObject object = new JSONObject(date);
            appno = object.optString("appNo");
            licenseNo = object.optString("licenseNo");
            pay_status = object.optString("payStatus");

            String returnCode = object.optString("returnCode");

            if (appno != null && returnCode.equals("10000")) {
                setContentView(R.layout.activity_vehicle_control);
                toolbar = (Toolbar) findViewById(R.id.toolbar);
                ToolBarHelper.setNormalTitleAndBack(VehicleControlActivity.this, toolbar, getString(R.string.car_control));
                ivLock = (LinearLayout) findViewById(R.id.iv_lock_ll);
                ivUnLock = (LinearLayout) findViewById(R.id.iv_lock_ll2);
                rlBackground = (RelativeLayout) findViewById(R.id.rl_vehicle_car);
                tv_return_car = (TextView) findViewById(R.id.tv_return_car);
                ivLock.setOnClickListener(VehicleControlActivity.this);
                ivUnLock.setOnClickListener(VehicleControlActivity.this);
                tv_return_car.setOnClickListener(VehicleControlActivity.this);
            } else {
//                setContentView(R.layout.activity_vehicle_control_null);

                Intent intent = new Intent(VehicleControlActivity.this, VehicleControlNullActivity.class);
                start_Activity(intent);
                finish();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void GetObtainCar() {
        JSONObject object = new JSONObject();
        boolean isLogin = SPUtils.getBoolean(this, "isLogin", false);
        String userPhone = SPUtils.getString(this, "LoginInfo", "");
        if (!isLogin || S.isEmpty(userPhone)) {
            UserUtils.cleanUserInfo(this);
            T.showShort("请先登录！");
            Intent intent = new Intent(VehicleControlActivity.this, LoginActivity.class);
            start_Activity(intent);
            return;
        }
        try {
            object.put("mobile", userPhone);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        loadingUtil = new LoadingUtil(this, getString(R.string.loading_text));
        loadingUtil.startShowLoading();
        OkHttpUtils.get().url(UrlValues.OBTAIN_CAR).addParams("input", object.toString()).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                loadingUtil.stopShowLoading();
                if (JsonUtil.isValid(response)) {

                    Obtain_car(response);
                } else {
                    Intent intent = new Intent(VehicleControlActivity.this, VehicleControlNullActivity.class);
                    start_Activity(intent);
                    finish();
                }
            }
        });
    }

    public void GetopenCatDoorParams(String control) {
        JSONObject object = new JSONObject();
        try {
            object.put("appNo", appno);
            object.put("licenseNo", licenseNo);
            object.put("ctrlFlag", control);
            object.put("positionLon", "110.13175");
            object.put("positionLat", "19.954046");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.post().url(UrlValues.CAR_VEHICLE_CONTROL).addParams("input", object.toString()).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
            }

            @Override
            public void onResponse(String response) {

                GetCarOpenDoorResponse(response);
            }
        });
    }

    public void GetCarOpenDoorResponse(String data) {
        if (JsonUtil.isValid(data)) {
            VehicleControlActivity.this.loadingUtil.stopShowLoading();
            if (isClose) {
                T.showLong("关门成功");
            } else if (isOpen) {
                T.showLong("开门成功");
            }
        }
    }

    public void ReturnCar() {
        JSONObject object = new JSONObject();
        try {
            String mobile = SPUtils.getString(this, "LoginInfo", null);
            if (mobile != null) {
                object.put("mobile", mobile);
                OkHttpUtils.post().url(UrlValues.TV_RETURN_CAR).addParams("input", object.toString()).build().execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(String response) {

                        getReturnCar(response);
                    }
                });
            } else {
                T.showShort(this, "信息有误");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void getReturnCar(String date) {
        if (JsonUtil.isValid(date)) {
            try {
                JSONObject jsonObject = new JSONObject(date);
                String resultCode = jsonObject.optString("returnCode");
                if (resultCode == null) {
                    T.showShort("返回数据有误");
                } else {
                    if ("10000".equals(resultCode)) {
                        T.showShort("还车成功");
                        finish();
                    } else {
                        T.showShort("还车失败");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_lock_ll:
                isClose = true;
                rlBackground.setBackgroundResource(R.mipmap.car_close);
                loadingUtil = new LoadingUtil(this, "关门中...");
                this.loadingUtil.startShowLoading();
                GetopenCatDoorParams("02");
                break;
            case R.id.iv_lock_ll2:
                isOpen = true;
                rlBackground.setBackgroundResource(R.mipmap.car_open);
                loadingUtil = new LoadingUtil(this, "开门中...");
                this.loadingUtil.startShowLoading();
                GetopenCatDoorParams("01");
                break;
            case R.id.tv_return_car:
                ReturnCar();
                break;
            default:
                break;
        }
    }

    protected Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {

            }
        }
    };
}
